package org.example.test_qlexpress.function;

import com.ql.util.express.Operator;

import java.math.BigDecimal;
import java.math.BigInteger;

public class CeilFunction extends Operator {
    public Object executeInner(Object[] objects) throws Exception {
        if (objects == null || objects.length == 0) {
            throw new RuntimeException("未传入任何参数。");
        }
        Object object = objects[0];
        if (object == null) {
            throw new RuntimeException("参数不能为null。");
        }
        if (object instanceof Byte
            || object instanceof Short
            || object instanceof Integer
            || object instanceof Long
            || object instanceof BigInteger) {
            return object;
        }
        if (object instanceof Float) {
            Float v = (Float) object;
            return Math.ceil(v);
        }
        if (object instanceof Double) {
            Double v = (Double) object;
            return Math.ceil(v);
        }
        if (object instanceof BigDecimal) {
            BigDecimal v1 = (BigDecimal) object;
            BigDecimal v2 = v1.setScale(0);
            if (v2.compareTo(v1) < 0) {
                return v2.add(BigDecimal.ONE);
            }
            return v1;
        }
        throw new RuntimeException("不支持的数据类型：" + object);
    }
}
